package com.ruoyi.project.monitor.job.task;

import com.alibaba.fastjson.JSONObject;
import com.arronlong.httpclientutil.HttpClientUtil;
import com.arronlong.httpclientutil.common.HttpConfig;
import com.arronlong.httpclientutil.common.HttpHeader;
import com.arronlong.httpclientutil.exception.HttpProcessException;
import com.ruoyi.common.utils.MD5Util;
import com.ruoyi.common.utils.PropertiesFileUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.module.packet.service.IPacketService;
import com.ruoyi.project.module.packetConfig.domain.PacketConfig;
import com.ruoyi.project.module.packetConfig.service.IPacketConfigService;
import com.ruoyi.project.system.config.service.IConfigService;
import org.apache.http.Header;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 定时任务调度
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{

    private static Logger _log = LoggerFactory.getLogger(RyTask.class);

    @Autowired
    private IPacketService packetService;

    @Autowired
    private IPacketConfigService packetConfigService;

    @Autowired
    private IConfigService configService;


    public void ryRedPacket_start10(String params){
        ryParams_start(params);
    }

    public void ryRedPacket_start12(String params){
        ryParams_start(params);
    }

    public void ryRedPacket_start14(String params){
        ryParams_start(params);
    }

    public void ryRedPacket_start16(String params){
        ryParams_start(params);
    }

    public void ryRedPacket_start18(String params){
        ryParams_start(params);
    }

    public void ryRedPacket_start20(String params){
        ryParams_start(params);
    }


    public void ryRedPacket_start22(String params){
        ryParams_start(params);
    }

    private void ryParams_start(String params)
    {
        _log.info("定时任务发送系统私人红包 start-------------------------------"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        String redPacketConfigId=configService.selectConfigByKey("sys.redpackconfig.valid");

        if(StringUtils.isEmpty(redPacketConfigId)){
            _log.info("请先在系统参数里面,配置有效红包的id");
            return;
        }


        _log.info(redPacketConfigId);
        //String redPacketConfigId="6E18CBE491CF476BB144E894FF1B3DA4";

        PacketConfig packetConfig = packetConfigService.selectPacketConfigById(redPacketConfigId);

        if(packetConfig==null){
            _log.info("请先配置有效红包的id");
            return;
        }
        Map<String, Object> map = new HashMap<String, Object>();

        BigDecimal redPacketAdmount = packetConfig.getRedPacketAdmount();
        if(redPacketAdmount!=null){
            map.put("redPacketAdmount", redPacketAdmount.toString());
        }
        map.put("redPacketRemark", packetConfig.getRedPacketRemark());
        map.put("redPacketPic", packetConfig.getRedPacketPic());
        Integer redPacketQuantity=packetConfig.getRedPacketQuantity();
        if(redPacketQuantity!=null){
            map.put("redPacketQuantity", redPacketQuantity.toString());
        }
        map.put("redPacketType", packetConfig.getRedPacketType());
        map.put("redPacketPw", packetConfig.getRedPacketPw());
        BigDecimal redPacketLon = packetConfig.getRedPacketLon();
        if(redPacketLon!=null){
            map.put("redPacketLon", redPacketLon.toString());
        }
        BigDecimal redPacketLat = packetConfig.getRedPacketLon();
        if(redPacketLat!=null){
            map.put("redPacketLat", redPacketLat.toString());
        }
        map.put("randomRules", packetConfig.getRandomRules());
        map.put("advertisingExternalLink", packetConfig.getAdvertisingExternalLink());
        map.put("advertisingPhone", packetConfig.getAdvertisingPhone());
        map.put("advertisingAddress", packetConfig.getAdvertisingAddress());
        map.put("redPacketUserId", packetConfig.getRedPacketUserId());
        map.put("redPacketStatus", packetConfig.getRedPacketStatus());
        map.put("redPacketSonArea", packetConfig.getRedPacketSonArea());
        map.put("userImg", packetConfig.getUserImg());

        String number = packetConfig.getRedPacketNumber().toString();

        map.put("number", number);

        String remoteServer_base_url = PropertiesFileUtil.getInstance("remoteServer").get("base_url");
        if(StringUtils.isEmpty(remoteServer_base_url)) {
            _log.info("请先配置有效远程url");
            return;
        }
        String url = remoteServer_base_url+"redPacket/addwebRedPacketNormal";
        String random= UUID.randomUUID().toString();
        String key = PropertiesFileUtil.getInstance("CheckSignKey").get("APP").toUpperCase();
        String randomSign= MD5Util.MD5(random.concat("&").concat(key));

        String dataSign = MD5Util.MD5(random.concat(number).concat(redPacketAdmount.toString()).concat(redPacketQuantity.toString()));
        Header[] headers 	= HttpHeader.custom()
                .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
                .other("random", random)
                .other("randomSign", randomSign)
                .other("dataSign", dataSign)
                .other("authorization", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
                .build();

        HttpConfig config = HttpConfig.custom()
                .headers(headers)	//设置headers，不需要时则无需设置
                .url(url)					//设置请求的url
                .map(map)			//设置请求参数，没有则无需设置
                .encoding("utf-8") //设置请求和返回编码，默认就是Charset.defaultCharset()
                ;
        String reponse_result = null;
        try {
            reponse_result = HttpClientUtil.post(config);
        } catch (HttpProcessException e) {
            e.printStackTrace();
        }

        JSONObject resjsStr = JSONObject.parseObject(reponse_result);
        if(resjsStr==null){
            _log.info("执行有参方法："+"返回为空,请注意配置base_url" + params+";" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }else{
            String msg=resjsStr.getString("msg");
            Integer code=Integer.parseInt(resjsStr.getString("code"));
            if(code.equals(0)){
                _log.info(msg);
            }
            _log.error(msg);

            _log.info("定时任务发送系统私人红包 end-------------------------------"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }




    }

    public void ryNoParams()
    {
        _log.info("执行无参方法");
    }

}
